package com.his.admin.controller;

import com.his.admin.service.HospitalService;
import com.his.admin.service.PatientService;
import com.his.admin.service.RegistrationService;
import com.his.admin.service.WebSocketService;
import com.his.dao.domain.Do.SelectAllRegistrationDo;
import com.his.dao.domain.dto.ListDto;
import com.his.dao.domain.dto.RegistrationNurseDto;
import com.his.dao.domain.entity.Hospital;
import com.his.dao.domain.entity.Registration;
import com.his.util.annotation.RequiresLogin;
import com.his.util.annotation.RequiresPermissions;
import com.his.util.result.RestResult;
import com.his.util.result.RestResultBuilder;
import com.his.util.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/registration")
@AllArgsConstructor
@Api(value = "挂号模块")
public class RegistrationController {
    private RegistrationService registrationService;
    private PatientService patientService;
    private WebSocketService webSocketService;

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all")
    @ApiOperation(value = "获取挂号列表",notes = "获取挂号列表")
    public RestResult all(@RequestBody ListDto listDto) {
        return registrationService.all(listDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/all/doctor")
    @ApiOperation(value = "获取与医生有关的挂号列表",notes = "获取与医生有关的挂号列表")
    public RestResult allDoctor(@RequestHeader("authorization") String authorization, @RequestBody ListDto listDto) {
        return registrationService.allDoctor(authorization, listDto);
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add")
    @ApiOperation(value = "增加挂号",notes = "增加挂号")
    public RestResult add(@RequestHeader("authorization") String authorization, @RequestBody Registration registration) {
        registration.setCreateTime(LocalDateTime.now());
        if (registrationService.save(registration)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/add/nurse")
    @ApiOperation(value = "增加挂号护士",notes = "增加挂号护士")
    public RestResult addNurse(@RequestHeader("authorization") String authorization, @RequestBody RegistrationNurseDto registrationNurseDto) {
        Integer patientId = patientService.getPatient(registrationNurseDto.getPatientCard(), registrationNurseDto.getPatientName());
        Registration registration = Registration
                .builder()
                .doctorId(registrationNurseDto.getDoctorId())
                .patientId(patientId)
                .date(registrationNurseDto.getDate())
                .isAppointment(registrationNurseDto.getIsAppointment())
                .isComplete(registrationNurseDto.getIsComplete())
                .isPayment(registrationNurseDto.getIsPayment())
                .createTime(LocalDateTime.now())
                .build();
        if (registrationService.save(registration)) {
            if (registration.getIsPayment() == 1 && registration.getIsAppointment() == 0) {
                SelectAllRegistrationDo selectAllRegistrationDo = (SelectAllRegistrationDo) registrationService.selectLast(registration).getData();

                webSocketService.sendRegistration(registration.getDoctorId(), JsonUtil.toJson(selectAllRegistrationDo));
            }
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/update")
    @ApiOperation(value = "更新挂号",notes = "更新挂号")
    public RestResult update(@RequestBody Registration registration) {
        registration.setUpdateTime(registration.getUpdateTime());
        if (registrationService.updateById(registration)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequiresLogin
    @RequiresPermissions
    @RequestMapping("/delete")
    @ApiOperation(value = "删除挂号",notes = "删除挂号")
    public RestResult delete(@RequestBody List<Integer> ids) {
        if (registrationService.removeByIds(ids)) {
            return new RestResultBuilder().success();
        }
        return new RestResultBuilder().fail();
    }

    @RequestMapping("/registration")
    @ApiOperation(value = "挂号",notes = "挂号")
    public RestResult registration(@RequestBody Registration registration) {
        SelectAllRegistrationDo selectAllRegistrationDo = (SelectAllRegistrationDo) registrationService.selectLast(registration).getData();

        webSocketService.sendRegistration(registration.getDoctorId(), JsonUtil.toJson(selectAllRegistrationDo));

        return new RestResultBuilder().success();
    }
}